package com.yuanqi.dz.dc.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import pnbclient.helper.StringHelper;
import pnbclient.util.PnbLogFactory;
import pnblog.PnbLogging;

import com.yuanqi.dz.dc.adapters.ReadExcel;
import com.yuanqi.dz.dc.util.DZConfigRead;
import com.yuanqi.dz.dc.util.SftpClientUtil;
import com.yuanqitech.advancedpower.powerdate.DateUtil;
import com.yuanqitech.advancedpower.powerdate.PowerDateFormat;
import com.yuanqitech.powermop.dz.DZDataInterface;
import com.yuanqitech.powermop.dz.DZInfo;
import com.yuanqitech.powermop.dz.DZInfoArray;

/**
 * 河北三院表格对账文件解析
 * @author HP
 *
 */
public class ReadXlsServiceImpl  implements DZDataInterface{

	public static final PnbLogging LOG = PnbLogFactory.getLogger("DZ_EXT_LOG");

	public static void main(String[] args) {
		new  ReadXlsServiceImpl().getDZData("105124000008205", "20180301");
	}

	public  DZInfoArray getDZData(String mercharnum, String dzDate){
		DZInfoArray dzInfoArray = null;
		Boolean download = false;
		SftpClientUtil sftp = null;
		String  is_private=DZConfigRead.getProperties("is_private");
		String source_type=null;
		String  address=null;
		String local_path =null;
		String fileName=null;
		String dz_date_format=null;
		String dz_date_addday=null;
		String ftp_port =null;
		String  username=null;
		String password = null;
		String  remotePath=null ;
		if(null!=is_private && "0".equals(is_private)){
			String  is_private_mercharnum=DZConfigRead.getProperties("is_private_"+mercharnum);
			if(null!=is_private_mercharnum && is_private_mercharnum.equals("1")){
				source_type=DZConfigRead.getProperties("source_type_"+mercharnum);
				address=DZConfigRead.getProperties("ftp_ip_"+mercharnum);
				local_path=DZConfigRead.getProperties("local_path_"+mercharnum);
				fileName=DZConfigRead.getProperties("fileName_"+mercharnum);
				dz_date_format=DZConfigRead.getProperties("dz_date_format_"+mercharnum);
				dz_date_addday=DZConfigRead.getProperties("dz_date_addday_"+mercharnum);
				ftp_port=DZConfigRead.getProperties("ftp_port_"+mercharnum);
				username=DZConfigRead.getProperties("ftp_username_"+mercharnum);
				password=DZConfigRead.getProperties("ftp_password_"+mercharnum);
				remotePath=DZConfigRead.getProperties("ftp_filepath_"+mercharnum);
			}else{
				source_type=DZConfigRead.getProperties("source_type");
				address=DZConfigRead.getProperties("ftp_ip");
				local_path=DZConfigRead.getProperties("local_path");//本地目录
				fileName=DZConfigRead.getProperties("fileName");// 文件名称
				dz_date_format=DZConfigRead.getProperties("dz_date_format");//对账文件时间格式
				dz_date_addday=DZConfigRead.getProperties("dz_date_addday");  
				ftp_port=DZConfigRead.getProperties("ftp_port");
				username=DZConfigRead.getProperties("ftp_username");
				password=DZConfigRead.getProperties("ftp_password");
				remotePath=DZConfigRead.getProperties("ftp_filepath");
			}

		}else{
			source_type=DZConfigRead.getProperties("source_type_"+mercharnum);
			address=DZConfigRead.getProperties("ftp_ip_"+mercharnum);
			local_path=DZConfigRead.getProperties("local_path_"+mercharnum);
			fileName=DZConfigRead.getProperties("fileName_"+mercharnum);
			dz_date_format=DZConfigRead.getProperties("dz_date_format_"+mercharnum);
			dz_date_addday=DZConfigRead.getProperties("dz_date_addday_"+mercharnum);
			ftp_port=DZConfigRead.getProperties("ftp_port_"+mercharnum);
			username=DZConfigRead.getProperties("ftp_username_"+mercharnum);
			password=DZConfigRead.getProperties("ftp_password_"+mercharnum);
			remotePath=DZConfigRead.getProperties("ftp_filepath_"+mercharnum);
		}
		int port = 21;
		if(!StringHelper.isEmpty(ftp_port)){
			port = Integer.valueOf(ftp_port);
		}
		SimpleDateFormat sdf = new SimpleDateFormat(dz_date_format);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(PowerDateFormat.getInstance(DateUtil.YYYYMMDD).parse(dzDate));
		fileName = fileName.replace("{merchant}", mercharnum).replace("{date}", sdf.format(calendar.getTime()));

		List<DZInfo> list = new ArrayList<DZInfo>();
		String path = local_path + fileName;
		try {
			LOG.addInfo("开始连接sftp:入参{address:"+address+",port:"+ftp_port+",username:"+username+",password:"+password+"}");
			sftp = new SftpClientUtil(address, port, username, password);
			sftp.connect();
			LOG.addInfo("从sftp下载对账文件,入参{remotePath:"+remotePath+",filename:"+fileName+",local_path:"+local_path+"}");
			download = sftp.download(remotePath, fileName, local_path);    
			if(!download){
				LOG.addError("下载对账文件失败");
			}
			File file = new File(local_path,fileName);
			if(file.exists()){
				list = new ReadExcel().readXls(path);
			}else{
				LOG.addError("文件 ："+file.getName() +"没有找到");
			}
			if(null == list || list.size()==0){


				list = new ReadExcel().readXls2(path);

				if(null == list || list.size()==0){
					dzInfoArray = new DZInfoArray();
					dzInfoArray.setRc("-1");
					dzInfoArray.setDzInfo(new DZInfo[0]);
					dzInfoArray.setErrormsg("获取对账数据为null");
					return dzInfoArray;
				}
			}


			for (int i = 0; i < list.size(); i++) {
				System.out.println(list.get(i));
			}

			dzInfoArray = new DZInfoArray();
			dzInfoArray.setRc("1");
			dzInfoArray.setDzInfo(list.toArray(new DZInfo[list.size()]));
			LOG.addInfo("获取对账数据"+list.size()+"条");
		} catch (Exception e) {
			LOG.addError(e);
			dzInfoArray = new DZInfoArray();
			dzInfoArray.setRc("-1");
			dzInfoArray.setDzInfo(new DZInfo[0]);
			dzInfoArray.setErrormsg("获取渠道对账数据异常");
			return dzInfoArray;
		}

		return dzInfoArray;
	}
}
